package cn.wolfcode.web.service.impl;

import cn.wolfcode.web.domain.Department;
import cn.wolfcode.web.mapper.DepartmentMapper;
import cn.wolfcode.web.query.QueryObject;
import cn.wolfcode.web.service.IDepartmentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public Department get(Long id) {
        Department department = departmentMapper.selectByPrimaryKey(id);
        return department;
    }

    @Override
    public List<Department> listAll() {

        List<Department> departments = departmentMapper.selectAll();
        return departments;
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    /*  @Override
      public PageInfo query(QueryObject qo) {

          int rows = departmentMapper.queryForCount(qo);
          if (rows == 0) {
              //        (int currentPage,int  pageSize,int  rows,List<?>  result)
              return new PageInfo(qo.getCurrentPage(), qo.getPageSize(), 0, Collections.emptyList());
          }

          List<Department> departmentList = departmentMapper.queryForList(qo);
          return new PageInfo(qo.getCurrentPage(), qo.getPageSize(), rows, departmentList);
      }
  */
    public PageInfo<Department> query(QueryObject qo) {
        //使用分页插件,调用插件方法传入当前页,每页显示数量,给它拼接,分工合成sql
//        它帮我们进行之前的判断,if (rows == 0)等操作; ,查询rows的总数据条数,我们也不用写了
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
//         调用sql,分页limit,插件帮我们自动拼接好了,分工合成sql
        List<Department> result = departmentMapper.queryForList(qo);
        //返回插件帮我们创建装了7个分页数据(curerentPage,nextPage等)的对象 PageInfo,
        // 也是相当于我们之前写的PageInfo对象
        return new PageInfo(result);//记得给它我查询出来的结果集合,不然他不知道是什么类型的集合
    }
}
